package TUILayer;
import java.util.*;
import ModelLayer.*;
import ControlLayer.*;


/**
 * Write a description of class EmployeeUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class EmployeeUI
{
    private static EmployeeCtrl employeeCtrl = new EmployeeCtrl();
    private static MainMenuUI mmu = new MainMenuUI();
    
    /**
     * Constructor for objects of class EmployeeUI
     */
    
    public EmployeeUI()
    {
        //employeeCtrl = new EmployeeCtrl();
    }
    
    public void startMainMenu()
    {
        boolean _exit = false;
        while (!_exit) //! means while exit not is true (that is: false)
        {
            int _choice = writeMainMenu();
            if(_choice == 1)
            {
                createEmployee();
            }
            else
            {
                if(_choice == 2)
                {
                    getEmployee();
                }
                else
                {
                    if(_choice == 3)
                    {
                        updateEmployee();
                    }
                    else
                    {
                        if(_choice == 4)
                        {
                            removeEmployee();
                        }
                        else 
                        {
                            if(_choice == 5)
                            {
                                listAllEmployees();
                            }
                            else
                            {
                                if(_choice == 6)
                                {
                                    mmu.mainMenuStart();
                                    _exit = true;
                                }
                                else
                                {
                                    
                                }
                            }
                        }
                    }
                }
            }
        }
    }
           
    private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** EMPLOYEE MENU ***");
            System.out.println(" (1) Create Employee");
            System.out.println(" (2) Show Employee Information");
            System.out.println(" (3) Update Employee");
            System.out.println(" (4) Delete Employee");
            System.out.println(" (5) List All Employees");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
    
    private long inputId()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Employee ID:  ");
           try
           {
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
           }
        }//end while
        
        return id;
    }
    
    private long inputCpr()
    {   
        boolean ok = false;
        long cpr = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Employee CPR:  ");
           try
           {
             cpr = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
           }
        }//end while
        
        return cpr;
    }
    
    private int inputSalary()
    {   
        boolean ok = false;
        int salary = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Employee salary:  ");
           try
           {
             salary = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
             //keyboard.nextLine();
           }
        }//end while
        
        return salary;
    }
    
    private String inputFirstName()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type first name: ");
        String firstName = keyboard.nextLine();
        
        return firstName;
    }
    
    private String inputLastName()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type last name: ");
        String lastName = keyboard.nextLine();
        
        return lastName;
    }
    
    private String inputBankAccount()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type bank account: ");
        String bankAccount = keyboard.nextLine();
        
        return bankAccount;
    }
    
    private String inputEmail()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type email: ");
        String email = keyboard.nextLine();
        
        return email;
    }
    
    private String inputPhone()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type phone: ");
        String phone = keyboard.nextLine();
        
        return phone;
    }
    
    private String inputAddress()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type address: ");
        String address = keyboard.nextLine();
        
        return address;
    }
    
    private String inputCity()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type city: ");
        String city = keyboard.nextLine();
        
        return city;
    }
    
    private String inputFunction()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type function: ");
        String function = keyboard.nextLine();
        
        return function;
    }
    
    private String inputCredentials()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type credentials: ");
        String credentials = keyboard.nextLine();
        
        return credentials;
    }
    
    private String inputDepartment()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type department: ");
        String department = keyboard.nextLine();
        
        return department;
    }
    
    private void getEmployee()
    {
        System.out.println(" --- Show Employee information ---");
        long id = inputId();
        if(employeeCtrl.getEmployee(id) != null)
        {
            Employee e = employeeCtrl.getEmployee(id);
            System.out.println("ID: " + e.getId());
            System.out.println("First name: " + e.getFirstName());
            System.out.println("Last name: " + e.getLastName());
            System.out.println("Bank account: " + e.getBankAccount());
            System.out.println("Email: " + e.getEmail());
            System.out.println("Phone: " + e.getPhone());
            System.out.println("Address: " + e.getAddress());
            System.out.println("City: " + e.getCity());
            System.out.println("CPR: " + e.getCpr());
            System.out.println("Function: " + e.getFunction());
            System.out.println("Salary: " + e.getSalary());
            System.out.println("Credentials: " + e.getCredentials());
            System.out.println("Department: " + e.getDepartment()); 
        }
        else
        {
            System.out.println("Employee with this ID does not exist!");
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void createEmployee()
    {
        System.out.println(" --- Create Employee ---");
        long id = inputId();
        if(employeeCtrl.getEmployee(id) == null)
        {
            Employee e = new Employee();
            e.setId(id);
            e.setFirstName(inputFirstName());
            e.setLastName(inputLastName());
            e.setBankAccount(inputBankAccount());
            e.setEmail(inputEmail());
            e.setPhone(inputPhone());
            e.setAddress(inputAddress());
            e.setCity(inputCity());
            e.setCpr(inputCpr());
            e.setFunction(inputFunction());
            e.setSalary(inputSalary());
            e.setCredentials(inputCredentials());
            e.setDepartment(inputDepartment());
            
            employeeCtrl.createEmployee(e);
            System.out.println("Employee has been created!");
        }
        
        else
        {
            System.out.println("Employee with this ID already exists");
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void updateEmployee()
    {
        System.out.println(" --- Update Employee ---");
        long id = inputId();
        if(employeeCtrl.getEmployee(id) != null)
        {
            Employee e = new Employee();
            e.setId(id);
            e.setFirstName(inputFirstName());
            e.setLastName(inputLastName());
            e.setBankAccount(inputBankAccount());
            e.setEmail(inputEmail());
            e.setPhone(inputPhone());
            e.setAddress(inputAddress());
            e.setCity(inputCity());
            e.setCpr(inputCpr());
            e.setFunction(inputFunction());
            e.setSalary(inputSalary());
            e.setCredentials(inputCredentials());
            e.setDepartment(inputDepartment());
            
            employeeCtrl.updateEmployee(e);
            System.out.println("Employee has been updated!");
        }
        
        else
        {
            System.out.println("Employee with this ID does not exist!");
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void removeEmployee()
    {
        System.out.println(" --- Remove Employee ---");
        long id = inputId();
        if(employeeCtrl.getEmployee(id) != null)
        {
            employeeCtrl.removeEmployee(id);
            System.out.println("Employee has been removed!");
        }
        else
        {
            System.out.println("Employee with this ID does not exist!");
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void listAllEmployees()
    {
        System.out.println(" --- List all Employees ---");
        if (employeeCtrl.listAllEmployees().size() == 0 )
        {
            System.out.println("There are no Employees in the system");
        }
        else
        {
            System.out.println("Results:");
            for (int i=0; i<employeeCtrl.listAllEmployees().size(); i++)
            {  
                System.out.println (i+1 +")" + "  ID: " + employeeCtrl.listAllEmployees().get(i).getId() + "  CPR: " + employeeCtrl.listAllEmployees().get(i).getCpr());
            }
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}
